package com.ktjy.t382.ch01.service.impl;

import com.ktjy.t382.ch01.mapper.SysUserMapper;
import com.ktjy.t382.ch01.pojo.SysUser;
import com.ktjy.t382.ch01.service.SysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysUserServiceImp implements SysUserService {
    private static final Logger logger = LoggerFactory.getLogger(SysUserServiceImp.class);
    // 定义 Mapper
    @Autowired
    SysUserMapper sysUserMapper;


    @Override
    public List<SysUser> findList(SysUser sysUser, Integer pageNo, Integer pageSize) {
        //计算起始索引
        Integer startIndex = (pageNo - 1) * pageSize;
        if (sysUser != null) {
            System.out.println("传入的sysUser对象的account属性值为：" + sysUser.getAccount());
        }
        return sysUserMapper.getSysUserList(sysUser, startIndex, pageSize);
    }

    @Override
    public Integer getSysUserCount(SysUser sysUser) {
        return sysUserMapper.getSysUserCount(sysUser);
    }

    @Override
    public int saveBatch(SysUser sysUserList) {
        return sysUserMapper.insert(sysUserList);
    }

    @Override
    public int updateBatch(SysUser sysUserList) {
        return sysUserMapper.update(sysUserList);
    }

    @Override
    public int deleteBatch(Integer idList) {
        return sysUserMapper.delete(idList);
    }

    @Override
    public SysUser getById(Integer id) {
        return sysUserMapper.getSysUserById(id);
    }

    @Override
    public SysUser getSysUserByAccountBiz(String account) {
        return sysUserMapper.getSysUserByAccount(account);
    }

    @Override
    public SysUser findByAccount(String account) throws Exception {
        try {
            return sysUserMapper.getByAccount(account);
        } catch (Exception e) {
            logger.error("执行 SysUserServiceImpl" +
                    ".findByAccount(String)方法出现异常.", e);
            throw e;
        }
    }
}
